package io.kljf.boot.common.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 响应数据
 *
 * @author katherine
 * @version 2020-11-16
 **/
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 编码：20000 表示成功，其他值表示失败
     */
    private  int code = 20000;


    /**
     * 消息内容
     */
    private String msg = "success";

    /**
     * 响应数据
     */
    private T data;

    public boolean success() {
        return code == 0 ? true : false;
    }

    public Result<T> ok(T data){
        this.setData(data);
        return this;
    }

    public Result<T> error(){
        this.code = 500;
        this.msg = "服务器内部错误";
        return this;
    }

    public Result<T> error(int code){
        this.code = code;
        // TODO 写个工具类
        this.msg = "服务器内部错误";
        return this;
    }

    public Result<T> error(int code,String msg){
        this.code =code;
        this.msg = msg;
        return  this;
    }

    public Result<T> error(String msg){
        this.code =500;
        this.msg = msg;
        return  this;
    }

}
